// pages/goods-specs/index.js
import {
    ReDistributionMall
} from "../../api/shipping.js"
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        order: {
            type: Boolean,
            value: true
        },
        shipping: {
            type: Boolean,
            value: false
        },
        pecs: {
            type: Boolean,
            value: false
        },
        btnText: {
            type: String,
            value: ''
        },
        dataObj: {
            type: Object,
            value: {}
        },
        // 规格商品列
        option: {
            type: Array,
            value: []
        },
        disabled: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        isShow: false,
        active: 0,
        activeChild: 0,
        goodsList: [{
            goodsImg: "/images/element_christmas_xpzq@2x.png",
            goodsName: "商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称",
            goodsPrice: "310.00",
            goodsSpecs: "规格规格规格个规格规格规格规格规格规格个规格规格规格",
            goodsSpecsCount: 2
        }, ],
        specsList: ['2.5kg', '3.5kg'],
        active: 0, //当前选中规格
        specsName: "",
        count: 1,
        showQi: false,
        thumb: '',
        property_price: '',
        propertydata: {}
    },

    observers: {
        'dataObj': function (newVal, oldVal) {
            console.log('接收到的对象：', newVal, oldVal, this.data.active, this.data.activeChild);
            if (this.data.dataObj.show == true) {

                this.data.active = 0
                this.data.activeChild = 0
                this.setData({
                    active: 0,
                    activeChild: 0
                })
            }

            let Array = ''
            let imgStr = ''
            if (this.data.dataObj.property[0].img != '' && this.data.dataObj.property[0].child.length > 0) {
                imgStr = this.data.dataObj.property && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild] && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].img != '' ? this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].img : this.data.dataObj.thumb;
            } else {
                imgStr = this.data.dataObj.property[this.data.activeChild].img ? this.data.dataObj.property[this.data.activeChild].img : this.data.dataObj.thumb
            }
            console.log('imgStr', imgStr)
            const imgArray = imgStr.split("|").map(img => {
                if (!img.startsWith("http://static.chonglaoban.cn/")) {
                    return "http://static.chonglaoban.cn/" + img;
                } else {
                    // 添加特定字符
                    return img;
                }
            });
            Array = imgArray.map(img => {
                if (!img.includes("http://static.chonglaoban.cn/")) {
                    return "http://static.chonglaoban.cn/" + img;
                } else {
                    return img;
                }
            });
            console.log('Array', Array)
            console.log("sss", this.data.dataObj)
            this.setData({
                thumb: newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].img != '' ? Array : Array,
                propeprice: newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].price ? newVal.property[0].child[0].price : newVal.property[0].price,
                propertydata: newVal.property[0].child[0],

            });
            // this.handleSelSpecs()
        },
        'show': function (newVal, oldVal) {
            this.data.active = 0
            this.data.activeChild = 0
            if (newVal && this.data.dataObj.tips == 0) { // 普通
                this.printImageInfo();
            } else if (newVal && this.data.dataObj.tips == 1) { //一件代发
                if (this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                    this.data.propertydata = {
                        productid: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                    }
                    let obj = {
                        id: this.data.dataObj.id,
                        propertyid: this.data.propertydata
                    }
                    this.getOneGoodsLists(obj)
                } else {
                    this.data.propertydata = {
                        productid: this.data.dataObj.propertyid
                    }
                    let obj = {
                        id: this.data.dataObj.id,
                        propertyid: this.data.propertydata
                    }
                    this.getOneGoodsLists(obj)
                }
            } else if (newVal && this.data.dataObj.tips == 2) { //京东
                // console.log('this.data.dataObj.property[this.data.active].child[this.data.activeChild]',this.data.active,this.data.activeChild,this.data.dataObj.property[this.data.active].child[this.data.activeChild].id)
                this.data.active = 0
                this.data.activeChild = 0
                if (this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                    this.data.propertydata = {
                        id: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                    }
                } else {
                    this.data.propertydata = {
                        id: this.data.dataObj.property[this.data.active].id
                    }
                }

            }
            this.setData({
                isShow: newVal
            })

        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        printImageInfo() {
            let Array = ''
            const imgStr = this.data.dataObj.property && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild] && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].img != '' ? this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].img : this.data.dataObj.thumb;
            const imgArray = imgStr.split("|").map(img => {
                if (!img.startsWith("http://static.chonglaoban.cn/")) {
                    return "http://static.chonglaoban.cn/" + img;
                } else {
                    // 添加特定字符
                    return img;
                }
            });
            Array = imgArray.map(img => {
                if (!img.includes("http://static.chonglaoban.cn/")) {
                    return "http://static.chonglaoban.cn/" + img;
                } else {
                    return img;
                }
            });
            console.log('Array2', Array)
            this.setData({
                // thumb: this.data.dataObj.property[this.data.active].child[this.data.activeChild].img || '',
                thumb: this.data.dataObj.property && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild] && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].img != '' ? Array : this.data.dataObj.thumb,
                // property_price: this.data.dataObj.property[this.data.active].child[this.data.activeChild].price
                property_price: this.data.dataObj.property && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild] ? this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].price : this.data.dataObj.property[this.data.activeChild].price,
                propeprice: this.data.dataObj.property && this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild] ? this.data.dataObj.property[this.data.activeChild].child[this.data.activeChild].price : this.data.dataObj.property[this.data.activeChild].price,
            });

        },

        //获取一件代发购物车商品数量
        getOneGoodsLists(item) {
            ReDistributionMall({
                act: 'oneGoodsInfo',
                id: item.id,
                propertyid: item.propertyid.productid

            }).then(res => {
                console.log('oneGoodsInfo', res)
                if (res.cart_numbers == '0') {
                    res.cart_numbers = 1
                }
                wx.nextTick(() => {
                    this.setData({
                        count: res.cart_numbers || 1
                    })
                })
            });
        },


        /**
         * 选择规格回调函数方法
         * @param {*} e 
         */
        handleSelSpecs(e) {

            console.log(this.data.dataObj, this.data.activeChild)

            const index = e.currentTarget.dataset.index;
            console.log('切换', e, this.data.dataObj.property, index)
            if (index == 1) {
                this.data.dataObj.show = false;
            }

            this.data.dataObj.propertyid = this.data.dataObj.property[index].child.length > 0 ? this.data.dataObj.property[index].child[this.data.activeChild].id : this.data.dataObj.property[index].id

            this.data.dataObj.propertyname = e.currentTarget.dataset.value.property_name

            let data = this.data.dataObj.property[index]
            if (data.child.length > 0) {
                if (data.child[this.data.activeChild].inventory == 0) {

                    this.setData({
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
                    this.setData({
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
            } else {
                if (data.inventory == 0) {

                    this.setData({
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
                    this.setData({
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
            }
            if (this.data.dataObj.property[index].img != '' && this.data.dataObj.property[index].child.length > 0) {
                if (this.data.dataObj.property[index].img != '' && this.data.dataObj.property[index].child[this.data.activeChild].img != '') {
                    let Array = ''
                    const imgStr = this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] && this.data.dataObj.property[index].child[this.data.activeChild].img ? Array : this.data.dataObj.property[index].img == '' ? this.data.dataObj.thumb : this.data.dataObj.property[index].img
                    const imgArray = imgStr.split("|").map(img => {
                        if (!img.startsWith("http://static.chonglaoban.cn/")) {
                            return img;
                        } else {
                            // 添加特定字符
                            return img;
                        }
                    });
                    Array = imgArray.map(img => {
                        if (!img.includes("http://static.chonglaoban.cn/")) {
                            return "http://static.chonglaoban.cn/" + img;
                        } else {
                            return img;
                        }
                    });
                }
            } else {
                //    let imgStr =  this.data.dataObj.property[index].img
                //     const imgArray = imgStr.split("|").map(img => {
                //         if (!img.startsWith("http://static.chonglaoban.cn/")) {
                //             return img;
                //         } else {
                //             // 添加特定字符
                //             return img;
                //         }
                //     });
                //     Array = imgArray.map(img => {
                //         if (!img.includes("http://static.chonglaoban.cn/")) {
                //             return "http://static.chonglaoban.cn/" + img;
                //         } else {
                //             return img;
                //         }
                //     });
            }

            console.log('Array=>>>>>>>>>>>>>>>>', Array)

            this.data.dataObj.real_price = this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].price : this.data.dataObj.property[index].price

            this.data.dataObj.property_price = this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].price : this.data.dataObj.property[index].price
            if (this.data.dataObj.property[index].img != '' && this.data.dataObj.property[index].child.length > 0) {
                if (this.data.dataObj.property[index].img != '' && this.data.dataObj.property[index].child[this.data.activeChild].img != '') {
                    this.data.dataObj.thumb = Array
                }
            } else {
                // this.data.dataObj.thumb = Array
            }


            this.setData({
                active: index,
                activeChild: 0, // 重置第二个规格栏的选中状态
                thumb: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] && this.data.dataObj.property[index].child[this.data.activeChild].img ? Array : this.data.dataObj.property[index].img == '' ? this.data.dataObj.thumb : this.data.dataObj.property[index].img,

                property_price: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].price : this.data.dataObj.property[index].price,

                count: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].number : this.data.dataObj.property[index].number,
                propeprice: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].price : this.data.dataObj.property[index].price,
                dataObj: this.data.dataObj,


            });
            // this.data.thumb = this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] && this.data.dataObj.property[index].child[this.data.activeChild].img ? Array : this.data.dataObj.property[index].img == '' ? this.data.dataObj.thumb : this.data.dataObj.property[index].img
            // this.setData({

            //     thumb:Array.join(",")
            // });
            let ee = {
                currentTarget: {
                    dataset: {
                        index: 0,
                        value: {
                            child: this.data.dataObj.property[index].child[0]

                        }
                    }
                }
            }
            // console.log
            if (this.data.dataObj.property[index].child.length > 0) {
                this.handleSelSpecsChild(ee)
            }



        },
        // handleSelSpecs(e) {
        //     const index = e.currentTarget.dataset.index;
        //     this.setData({
        //         active: index,
        //         activeChild: 0, // 重置第二个规格栏的选中状态
        //         thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
        //         property_price:this.data.dataObj.property[index].child[this.data.activeChild].price
        //     });

        // },
        handleSelSpecsChild(e) {
            console.log('子', e)
            const index = e.currentTarget.dataset.index;
            let data = this.data.dataObj.property[this.data.active].child[index]
            if (data.inventory == 0) {

                this.setData({
                    disabled: true,
                    btnText: "已售罄"
                })
            } else {
                this.setData({
                    disabled: false,
                    btnText: "加入购物车"
                })
            }
            let Array = ''
            if (this.data.dataObj.property[this.data.active].child[index].img != '') {
                const imgStr = this.data.dataObj.property[this.data.active].child[index].img == '' ? this.data.dataObj.property[this.data.active].img : this.data.dataObj.property[this.data.active].child[index].img
                const imgArray = imgStr.split("|").map(img => {
                    if (!img.startsWith("http://static.chonglaoban.cn/")) {
                        return img;
                    } else {
                        // 添加特定字符
                        return img;
                    }
                });
                Array = imgArray.map(img => {
                    if (!img.includes("http://static.chonglaoban.cn/") && !img.includes("http://img13.360buyimg.com/")) {
                        return "http://static.chonglaoban.cn/" + img;
                    } else {
                        return img;
                    }
                });
            }
            console.log('Arraysss', Array, this.data.active, index, this.data.dataObj.property[this.data.active].child[index].img != '')
            if (this.data.dataObj.property[this.data.active].child[index].img != '') {
                this.data.dataObj.thumb = ''
                this.data.dataObj.thumb = Array.toString()
            }
            console.log('Arraysss', this.data.dataObj.thumb)

            this.data.dataObj.real_price = this.data.dataObj.property[this.data.active].child[index].price

            this.data.dataObj.property_price = this.data.dataObj.property[this.data.active].child[index].price
            this.setData({
                activeChild: index,
                // property_price: this.data.dataObj.property[this.data.active].child[index].price,
                // propeprice:this.data.dataObj.property[this.data.active].child[index].price,

                property_price: this.data.dataObj.property[this.data.active].child[index].price || this.data.dataObj.property[this.data.active].price,
                propeprice: this.data.dataObj.property[this.data.active].child[index].price || this.data.dataObj.property[this.data.active].price,
                dataObj: this.data.dataObj,
                propeprice: this.data.dataObj.property[this.data.active].child[index].price || this.data.dataObj.property[this.data.active].price,
            });
            console.log('e', this.data.dataObj)
            this.data.propertydata = {
                ...e.currentTarget.dataset.value
            }
            console.log('this.data.dataObj.tips == 2 && this.data.dataObj.inventory != 0', this.data.dataObj.tips == 2 && this.data.dataObj.inventory != 0)
            if (this.data.dataObj.tips == 2 && this.data.dataObj.inventory != 0) {
                this.setData({
                    thumb: this.data.dataObj.thumb,
                })
            }
        },
        /**
         * 商品数量变化回调函数方法
         * @param {*} e 
         */
        handleNumChange(e) {
            this.setData({
                count: e.detail
            })
        },
        /**
         * 确认按钮回调函数方法
         */
        onConfirm() {
            let num
            // tips = 0商品列表 tips = 1 一件代发 
            console.log('this.data.dataObj.tips', this.data.dataObj)
            if (this.data.dataObj.tips == 1) {
                num = 1
            } else {
                num = 0
            }
            let proerid = ''
            if (this.data.dataObj.propertyid != '' && this.data.dataObj.propertyid != undefined && this.data.dataObj.propertyid != null) {
                proerid = this.data.dataObj.propertyid
            } else {
                proerid = this.data.dataObj.property && this.data.dataObj.property[this.data.active].child[this.data.activeChild] && this.data.dataObj.property[this.data.active].child[this.data.activeChild].id ? this.data.dataObj.property[this.data.active].child[this.data.activeChild].id : this.data.dataObj.property[this.data.active].id
            }
            this.triggerEvent('confirm', {
                index: this.data.active,
                name: this.data.specsList[this.data.active],

                data: {
                    ...this.data.dataObj,
                    propertyid: proerid
                },
                count: this.data.count || 1,
                tips: num,
                commodityId: this.data.dataObj.id
            })
        },
        /**
         * 关闭
         */
        close() {
            this.triggerEvent('close')
        }
    }
})